/*
 * @文件描述: 
 * @版本: 
 * @作者: 周晨阳
 * @Date: 2021-04-18 14:59:01
 */
#ifdef GUI_UCGUI

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "multiMeterUICodes.h"
#include "HomePagePre.h"

#include "BUTTON.h"
#include "CHECKBOX.h"
#include "DIALOG.h"
#include "DROPDOWN.h"
#include "EDIT.h"
#include "FRAMEWIN.h"
#include "GUI.h"
#include "LISTBOX.h"
#include "LISTVIEW.h"
#include "MULTIEDIT.h"
#include "PROGBAR.h"
#include "RADIO.h"
#include "SCROLLBAR.h"
#include "SLIDER.h"
#include "TEXT.h"
#include "WM.h"


#define MODE_VOLTAGE 0
#define MODE_CURRENT 1
#define MODE_RESISTANCE 2

#define countof(Obj) (sizeof(Obj) / sizeof(Obj[0]))
#define DEG2RAD (3.1415926f / 180)

// EventsFunctionList
void OnChgRangeClicked(WM_MESSAGE *pMsg);
void OnHomeClicked(WM_MESSAGE *pMsg);
void OnHoldClicked(WM_MESSAGE *pMsg);
void OnCurrentClicked(WM_MESSAGE *pMsg);
void OnResClicked(WM_MESSAGE *pMsg);
void OnVoltageClicked(WM_MESSAGE *pMsg);
// EndofEventsFunctionList

/*********************************************************************
 *
 *       static data
 *
 **********************************************************************
 */
static WM_HWIN mainPageHWIN;
static GUI_AUTODEV AutoDev;
static GUI_AUTODEV_INFO AutoDevInfo;
extern GUI_CONST_STORAGE GUI_BITMAP bmmultimeterGUI;
static float niddelAngle = 15.0f;
static const GUI_POINT niddels[4] = {
    {-2, 0},
    {-2, -467},
    {2, -467},
    {2, 0}};
/*********************************************************************
 *
 *       Dialog resource
 *
 * This table conatins the info required to create the dialog.
 * It has been created by ucGUIbuilder.
 */
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
    {FRAMEWIN_CreateIndirect, "MultiMeter", 0, 0, 0, 335, 480, FRAMEWIN_CF_MOVEABLE, 0},
    {BUTTON_CreateIndirect, "Voltage", GUI_ID_BUTTON0, 6, 24, 57, 39, 0, 0},
    {BUTTON_CreateIndirect, "Resistance", GUI_ID_BUTTON1, 6, 142, 57, 42, 0, 0},
    {BUTTON_CreateIndirect, "Current", GUI_ID_BUTTON2, 6, 263, 57, 44, 0, 0},
    {TEXT_CreateIndirect, "value", GUI_ID_TEXT1, 83, 77, 168, 85, 0, 0},
    {BUTTON_CreateIndirect, "HOLD", GUI_ID_BUTTON3, 244, 388, 68, 39, 0, 0},
    {BUTTON_CreateIndirect, "Home", GUI_ID_BUTTON4, 6, 386, 57, 41, 0, 0},
    {TEXT_CreateIndirect, "V", GUI_ID_TEXT0, 248, 72, 77, 90, 0, 0},
    {BUTTON_CreateIndirect, "ChangeRange", GUI_ID_BUTTON5, 95, 390, 84, 39, 0, 0},
    {TEXT_CreateIndirect, "Range:", GUI_ID_TEXT2, 83, 320, 68, 36, 0, 0},
    {TEXT_CreateIndirect, "-100 - +100", GUI_ID_TEXT3, 157, 309, 175, 47, 0, 0}};

/**
 * @brief : 绘制指针
 * @param {*}
 * @return {*}
 */
void drawNiddle(float angle)
{
  GUI_POINT newNiddel[4];

  GUI_SetColor(0xdd2c00);
  GUI_RotatePolygon(newNiddel, niddels, 4, -angle * DEG2RAD);
  GUI_FillPolygon(newNiddel, 4, 594, 555);
}

/**
 * @brief : 绘制背景
 * @param {*}
 * @return {*}
 */
static void drawBG()
{
  GUI_DrawBitmapMag(&bmmultimeterGUI, 334, 24, 2, 2);
}
/**
 * @brief : 绘制指针
 * @param {void} *p
 * @return {*}
 */
static void drawLoop(void *p)
{
  if (AutoDevInfo.DrawFixed)
  {
    drawBG();
    //drawNiddle(niddelAngle);
  }
  drawNiddle(niddelAngle);
}
/*****************************************************************
**      FunctionName:void PaintDialog(WM_MESSAGE * pMsg)
**      Function: to initialize the Dialog items
**
**      call this function in _cbCallback --> WM_PAINT
*****************************************************************/

static void PaintDialog(WM_MESSAGE *pMsg)
{
  WM_HWIN hWin = pMsg->hWin;
  GUI_SetColor(0x000000);
  GUI_DrawLine(69, -1, 69, 451);
  GUI_SetColor(0x000000);
  GUI_DrawLine(69, 253, 331, 253);
  GUI_SetColor(0x000000);
  GUI_DrawLine(331, -1, 331, 448);
}

/*****************************************************************
**      FunctionName:void InitDialog(WM_MESSAGE * pMsg)
**      Function: to initialize the Dialog items
**
**      call this function in _cbCallback --> WM_INIT_DIALOG
*****************************************************************/

static void InitDialog(WM_MESSAGE *pMsg)
{
  WM_HWIN hWin = pMsg->hWin;
  //
  // FRAMEWIN
  //
  FRAMEWIN_SetTextAlign(hWin, GUI_TA_VCENTER | GUI_TA_CENTER);
  FRAMEWIN_SetTitleHeight(hWin, 24);
  //
  // GUI_ID_BUTTON0
  //
  //  BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON0),
  //  BUTTON_CI_UNPRESSED, 0xff9933);
  //
  // GUI_ID_BUTTON1
  //
  // BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON1),
  // BUTTON_CI_UNPRESSED, 0x0080ff);
  //
  // GUI_ID_BUTTON2
  //
  // BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON2),
  // BUTTON_CI_UNPRESSED, 0x00ff00);
  //
  // GUI_ID_TEXT1
  //
  TEXT_SetTextColor(WM_GetDialogItem(hWin, GUI_ID_TEXT1), 0x008cff);
  TEXT_SetFont(WM_GetDialogItem(hWin, GUI_ID_TEXT1), &GUI_FontD32);
  TEXT_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_TEXT1),
                    GUI_TA_VCENTER | GUI_TA_CENTER);
  //
  // GUI_ID_BUTTON3
  //
  BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), BUTTON_CI_UNPRESSED,
                    0x0000ff);
  BUTTON_SetBkColor(WM_GetDialogItem(hWin, GUI_ID_BUTTON3), BUTTON_CI_PRESSED,
                    0xff9933);
  //
  // GUI_ID_TEXT0
  //
  TEXT_SetFont(WM_GetDialogItem(hWin, GUI_ID_TEXT0), &GUI_Font24_ASCII);
  TEXT_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_TEXT0),
                    GUI_TA_VCENTER | GUI_TA_CENTER);
  //
  // GUI_ID_TEXT2
  //
  TEXT_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_TEXT2),
                    GUI_TA_VCENTER | GUI_TA_CENTER);
  //
  // GUI_ID_TEXT3
  //
  TEXT_SetFont(WM_GetDialogItem(hWin, GUI_ID_TEXT3), &GUI_Font24_ASCII);
  TEXT_SetTextAlign(WM_GetDialogItem(hWin, GUI_ID_TEXT3),
                    GUI_TA_VCENTER | GUI_TA_CENTER);
}
/**
 * @brief : 背景窗口回调
 * @param {*}
 * @return {*}
 */
static void cbBgWin(WM_MESSAGE *pMsg)
{
  switch (pMsg->MsgId)
  {
  case WM_PAINT:
    /* code */
    drawBG();
    //drawNiddle(0);
    //printf("cbBgWin WM_PAINT\n");
    break;

  default:
    WM_DefaultProc(pMsg);
    // printf("WM_DefaultProc\n");
    break;
  }
}

/*********************************************************************
 *
 *       Dialog callback routine
 */
static void _cbCallback(WM_MESSAGE *pMsg)
{
  int NCode, Id;
  WM_HWIN hWin = pMsg->hWin;
  switch (pMsg->MsgId)
  {
  case WM_PAINT:
    PaintDialog(pMsg);
    break;
  case WM_INIT_DIALOG:
    InitDialog(pMsg);
    break;
  case WM_KEY:
    switch (((WM_KEY_INFO *)(pMsg->Data.p))->Key)
    {
    case GUI_KEY_ESCAPE:
      GUI_EndDialog(hWin, 1);
      break;
    case GUI_KEY_ENTER:
      GUI_EndDialog(hWin, 0);
      break;
    }
    break;
  case WM_NOTIFY_PARENT:
    Id = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch (Id)
    {
    case GUI_ID_OK:
      if (NCode == WM_NOTIFICATION_RELEASED)
        GUI_EndDialog(hWin, 0);
      break;
    case GUI_ID_CANCEL:
      if (NCode == WM_NOTIFICATION_RELEASED)
        GUI_EndDialog(hWin, 0);
      break;
    case GUI_ID_BUTTON0:
      switch (NCode)
      {
      case WM_NOTIFICATION_CLICKED:
        OnVoltageClicked(pMsg);
        break;
      }
      break;
    case GUI_ID_BUTTON1:
      switch (NCode)
      {
      case WM_NOTIFICATION_CLICKED:
        OnResClicked(pMsg);
        break;
      }
      break;
    case GUI_ID_BUTTON2:
      switch (NCode)
      {
      case WM_NOTIFICATION_CLICKED:
        OnCurrentClicked(pMsg);
        break;
      }
      break;
    case GUI_ID_BUTTON3:
      switch (NCode)
      {
      case WM_NOTIFICATION_CLICKED:
        OnHoldClicked(pMsg);
        break;
      }
      break;
    case GUI_ID_BUTTON4:
      switch (NCode)
      {
      case WM_NOTIFICATION_CLICKED:
        OnHomeClicked(pMsg);
        GUI_EndDialog(mainPageHWIN, 0);
        V_goHome();

        break;
      }
      break;
    case GUI_ID_BUTTON5:
      switch (NCode)
      {
      case WM_NOTIFICATION_CLICKED:
        OnChgRangeClicked(pMsg);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
 *
 *       MultiMeterUI_Init
 *
 **********************************************************************
 */
void MultiMeterUI_Init(void)
{

  WM_SetCallback(WM_HBKWIN, cbBgWin);
  GUI_MEMDEV_CreateAuto(&AutoDev);
  mainPageHWIN = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbCallback,
                                     0, 0, 0);
}

static char dispBuff[15];
/**
 * @description: UI显示数数字
 * @param {float} num
 * @return {*}
 */
void UIsetNum(float num, unsigned char digit)
{
  if (digit == 2)
  {
    sprintf(dispBuff, "%.2f", num);
  }
  else if (digit == 1)
  {
    sprintf(dispBuff, "%.1f", num);
  }
  else if (digit == 3)
  {
    sprintf(dispBuff, "%.3f", num);
  }
  else
  {
    sprintf(dispBuff, "%.1f", num);
  }
  TEXT_SetText(WM_GetDialogItem(mainPageHWIN, GUI_ID_TEXT1), dispBuff);
}
/**
 * @description: UI显示单位
 * @param {char} *unit
 * @return {*}
 */
void UIsetDisUnit(char *unit)
{
  // sprintf(dispBuff, "%f", num);
  // printf("ui unit is %s\n", unit);
  TEXT_SetText(WM_GetDialogItem(mainPageHWIN, GUI_ID_TEXT0), unit);
}
/**
 * @description: UI设置当前显示的模式
 * @param {int} mode 显示的模式，电压表，电流表，欧姆表
 * @return {*}
 */
void UIsetMode(int mode)
{
  switch (mode)
  {
  case MODE_VOLTAGE:
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON0),
                      BUTTON_CI_UNPRESSED, 0xff9933);

    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON1),
                      BUTTON_CI_UNPRESSED, 0xB6B6AA);
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON2),
                      BUTTON_CI_UNPRESSED, 0xB6B6AA);

    break;
  case MODE_CURRENT:
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON2),
                      BUTTON_CI_UNPRESSED, 0xff9933);

    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON1),
                      BUTTON_CI_UNPRESSED, 0xB6B6AA);
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON0),
                      BUTTON_CI_UNPRESSED, 0xB6B6AA);

    break;
  case MODE_RESISTANCE:
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON1),
                      BUTTON_CI_UNPRESSED, 0xff9933);

    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON0),
                      BUTTON_CI_UNPRESSED, 0xB6B6AA);
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON2),
                      BUTTON_CI_UNPRESSED, 0xB6B6AA);

    break;
  }
}

/**
 * @description: UI设置显示的量程
 * @param {char} *range 量程的字符串
 * @return {*}
 */
void UIsetRange(char *range)
{
  TEXT_SetText(WM_GetDialogItem(mainPageHWIN, GUI_ID_TEXT3), range);
}
/**
 * @description: UI设置运行状态
 * @param {int} state
 * @return {*}
 */
void UIsetRunningState(int state)
{
  if (!state)
  {
    BUTTON_SetText(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON3), "Release");
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON3), 0,
                      0x008000);
  }
  else
  {
    BUTTON_SetText(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON3), "HOLD");
    BUTTON_SetBkColor(WM_GetDialogItem(mainPageHWIN, GUI_ID_BUTTON3), 0,
                      0x0000ff);
  }
}
void UISetAngle(float angle)
{
  niddelAngle = angle;
}
void onViewDrawNiddel()
{
  GUI_MEMDEV_DrawAuto(&AutoDev, &AutoDevInfo, drawLoop, NULL);
}
#endif

